class TreeNode():
    def __init__(self,val=0,left=None,right=None):
        self.val=val 
        self.left=left 
        self.right=right

from collections import deque
def build_level(lst):
    root=TreeNode(lst[0])
    que=deque([root])
    i=1 
    while len(que)>0 and i<len(lst):
        cur=que.popleft()
        if lst[i]: 
            cur.left=TreeNode(lst[i])
            que.append(cur.left)
        if i+1<len(lst) and lst[i+1]:
            cur.right=TreeNode(lst[i+1])
            que.append(cur.right)
        i+=2 
    return root 


def isValidBST(root,pre):
    if not root: return True 
    if not isValidBST(root.left,pre) or root.val<=pre: return False 
    pre=root.val 
    return isValidBST(root.right,pre)

def solve(root):
    return isValidBST(root,-float('inf'))

root=build_level([2,1,3])
print(solve(root))
root=build_level([5,1,4,None,None,3,6])
print(solve(root))